<?php

namespace App\Admin\Controllers;

use App\Admin\Export\UsersExport;
use App\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class UserController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '医生管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User());

        $grid->column('id', __('Id'));
        $grid->column('name', __('Name'));
        $grid->column('email', __('Email'));
        $grid->column('email_verified_at', __('Email verified at'));
        $grid->column('password', __('Password'));
        $grid->column('remember_token', __('Remember token'));
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));


        /*****   自定义筛选   *****/
        $grid->filter(function ($filter){
            $filter->disableIdFilter();
            $filter->in('id','随便')->multipleSelect([1=>5,2=>2,3=>3]);
            $filter->between('created_at', '创建时间')->datetime(); //根据创建时间筛选sql: ... WHEREcolumnBETWEEN "$start" AND "$end"：

//            $filter->equal('name', '名字严格筛选');
            $filter->where(function ($query){
                $query->where('name','like',$this->input)
                    ->orwhere('email','like',"%{$this->input}%");
            },'检索词')->placeholder('名字/邮箱筛选');
        });


        /*****   自定义导出   *****/
        $grid->exporter(new UsersExport());



        /*****   自定义增加表单   *****/
        $grid->header($this->add_other_export_form());//待定，页面没有显示出来新增的表单

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('Name'));
        $show->field('email', __('Email'));
        $show->field('email_verified_at', __('Email verified at'));
        $show->field('password', __('Password'));
        $show->field('remember_token', __('Remember token'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User());

        $form->text('name', __('Name'));
        $form->email('email', __('Email'));
        $form->datetime('email_verified_at', __('Email verified at'))->default(date('Y-m-d H:i:s'));
        $form->password('password', __('Password'));
        $form->text('remember_token', __('Remember token'));

        return $form;
    }


    protected function add_other_export_form(){
         $form = new Form(new User());
         $form->datetime('email_verified_at');
         $form->setAction('/admin/export/user-info');
    }
}
